package cn.codesugar.multimedia

import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.NotificationManager.IMPORTANCE_HIGH
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.NotificationCompat
import cn.codesugar.multimedia.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private var b: ActivityMainBinding? = null
    private val channelId = "0x001"
    private val channelName = "渠道广播"

    @SuppressLint("UnspecifiedImmutableFlag")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        b = ActivityMainBinding.inflate(layoutInflater)
        setContentView(b?.root)
        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        // 通知渠道，Android 8.0 新增API
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val importance = IMPORTANCE_HIGH
            val channel = NotificationChannel(channelId, channelName, importance)
            notificationManager.createNotificationChannel(channel)
        }
        val intent = Intent(this, NotificationActivity::class.java)
        b?.sendNotice?.setOnClickListener {
            val pi = PendingIntent.getActivity(this, 0, intent, 0)
            val notification = NotificationCompat.Builder(this, channelId)
                .setContentTitle("This is content title")
                .setContentText("""Learn how to build notifications, send and sync data,
                    |and use voice actions.Get the official Android IDE and developer
                    |tools to build apps for Android.""".trimMargin())
                .setSmallIcon(R.drawable.ic_android_vector)
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.ic_android_vector))
                .setContentIntent(pi)
//                .setAutoCancel(true)
                // 5.0 不支持，未成功测试
//                .setStyle(NotificationCompat
//                    .BigTextStyle()
//                    .bigText("""Learn how to build notifications, send and sync data,
//                        |and use voice actions.Get the official Android IDE and developer
//                        |tools to build apps for Android.""".trimMargin()))

//                .setStyle(NotificationCompat
//                    .BigPictureStyle()
//                    .bigPicture(BitmapFactory
//                        .decodeResource(
//                            resources,
//                            R.mipmap.ic_launcher_round
//                        )
//                    )
//                )
                .build()
            notificationManager.notify(1, notification)
        }
        b?.testCameraAlbum?.setOnClickListener {
            startActivity(Intent(this, CameraAlbumTestActivity::class.java))
        }
        b?.testPlayAudio?.setOnClickListener {
            startActivity(Intent(this, PlayAudioTestActivity::class.java))
        }
        b?.testPlayVideo?.setOnClickListener {
            startActivity(Intent(this, PlayVideoTestActivity::class.java))
        }
    }
}